﻿var isModify;//是否修改
var sTR;
$(initPage);
function initPage() {
	//初始化翻页组件
    trunPageObj =  $(".pageNext").turnPage();
	//新增
	$("#btnQuery").click(function() {
		$("#btnEdit,#btnSave,#btnCancle").attr("disabled", true);
		trunPageObj.gotoPage(1);//搜索第一页
	});
	
	//修改
	$("#btnEdit").click(function() {
		initEdit();
		$("#btnEdit").attr("disabled", true);
		$("#btnSave,#btnCancle").attr("disabled",false);
		isModify = true;
	});
	
	//取消
	$("#btnCancle").click(function() {
		$("#btnEdit,#btnSave,#btnCancle").attr("disabled", true);
		initCancel();
		clearValues();
	});
	
	//保存
	$("#btnSave").click(function() {
		saveInfo();
	});
	$("#endDateId").attr("value",addDays(new Date(),30).dateString);
	queryList();
}

//清除表单值
function clearValues() {
	$("tr","#data_list").attr("bgColor","#FFFFFF");
}

//保存
function saveInfo() {
	$.post("m/submit_conf/modify_cale", {
			dateId:$.trim($("#dateId").val()),
			tradeFlag:$.trim($("#tradeFlag").val()),
			incrementFlag:$.trim($("#incrementFlag").val())
		}, function(data) {
			if ($.checkErrorMsg(data) ) {
				alert("系统提示：保存成功！");
				$("#btnCancle").click();
				queryList();
			} 
		}, "json");
}
function queryList(){
	var startDateId = $("#startDateId").val();
	var endDateId = $("#endDateId").val();
	if ($.compareDate(startDateId, endDateId)) {
		alert("开始日期不能大于结束日期");
		return;
	}
	var pageno=trunPageObj.getCurrentPageNo();//当前第几页
	var pagecount=trunPageObj.getPageCount();//一页多少条
	$.post("m/submit_conf/query_cale", {
		pageNo: pageno,
		pageSize : pagecount,
		startDateId : startDateId,
		endDateId : endDateId
	}, function(data) {
		if ($.checkErrorMsg(data) ) {
			var fileList = [];
			if(data.submitCalendarPage && data.submitCalendarPage.dataList){
				var dataList = data.submitCalendarPage.dataList;
				$(dataList).each(function(i) {
					var incrementFlag = dataList[i].incrementFlag == 0? "全量":"增量";
					var tradeFlag = dataList[i].tradeFlag == 1? "是":"否";
					fileList.push("<tr class='table_td_font' bgColor='#ffffff' onclick='selectTR(this)'>");
					fileList.push("<td align='center'  height='25px' class='queryList_linebg' nowrap value='" + dataList[i].dateId + "'>" + dataList[i].formatDateId + "</td>");
					fileList.push("<td align='left'  height='25px' class='queryList_linebg' nowrap >"+dataList[i].formatWeekdayNo+"</td>");
					fileList.push("<td align='center'  height='25px' class='queryList_linebg' nowrap value='"+dataList[i].tradeFlag+"'>"+tradeFlag+"</td>");
					fileList.push("<td align='center'  height='25px' class='queryList_linebg' nowrap value='"+dataList[i].incrementFlag+"'>" + incrementFlag + "</td>");
					fileList.push("</tr>");
				});
			}
			$("#data_list").html(fileList.join(""));
			trunPageObj.setPageData(data.submitCalendarPage);
		} 
	}, "json");
	$("#btnCancle").click();
}

function initEdit(){
	if(sTR == null){
		alert("请选择您要修改的记录");
		return;
	}
	var tds = $(sTR).find("td");
	$(tds[2]).html("<input type='hidden' id='dateId' value='"+tds.eq(0).attr("value")+"'><select id='tradeFlag' style='width:80px;'><option value='1'>是</option><option value='0'>否</option></select>");
    $(tds[3]).html("<select id='incrementFlag' style='width:80px;'><option value='0'>全量</option><option value='1'>增量</option></select>");
    $("#tradeFlag").val(tds.eq(2).attr("value"));
    $("#incrementFlag").val(tds.eq(3).attr("value"));
}

function initCancel(){
	if(sTR == null){
		return;
	}
	var tds = $(sTR).find("td");
	var tradeFlag = tds.eq(2).attr("value");
	var incrementFlag = tds.eq(3).attr("value");
	tradeFlagText = tradeFlag == 1? "是":"否";
	incrementFlagText = incrementFlag == 0 ? "全量":"增量";
	$(tds[2]).html(tradeFlagText);
    $(tds[3]).html(incrementFlagText);
    sTR = null;
}
function selectTR(obj) {
	if(obj == sTR){
		return;
	}
	if($("#btnSave").attr("disabled") == false) {
		if (!confirm("当前编内容还未保存，是否继续？")) {
			return;
		}else{
			initCancel();
		}
	}
	$(obj).attr("bgColor","#FAEBD7").siblings().attr("bgColor","#FFFFFF");
	var tds = $(obj).find("td");
	$("#btnSave,#btnCancle").attr("disabled", true);
	$("#btnEdit").attr("disabled",false);
	sTR = obj;
}
    /* 
     * 返回当前时间加上几天后的时间
     * 如'2008-12-31' + 2 = '2009-1-2'
     */
    function addDays(mydate, days){
        var currentDate = mydate;
        var newDate = new Date(Date.parse(currentDate) + 86400000 * days);
        var newMonth = (newDate.getMonth() + 1) < 10?("0"+(newDate.getMonth() + 1)):(newDate.getMonth() + 1);
        var newDay = newDate.getDate() < 10?("0"+newDate.getDate()):newDate.getDate();
        return {
            "date": newDate,
            "dateString": newDate.getFullYear() + "-" + newMonth + "-" + newDay
        }
    }